package com.UtilDate;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;
import com.shareup.R;


public class UtilDate {

    private final Context context;

    public interface OnSetDifferenceCompleted {

        void Completed(String diff);
    }

    class SetDifference extends AsyncTask<String, String, String> {

        private static final int Month = 30;

        private static final int Year = 12;

        private OnSetDifferenceCompleted callback;

        private final String date;

        private TextView text;

        public SetDifference(String date,
                    OnSetDifferenceCompleted callback) {

            super();
            this.date = date;
            this.callback = callback;
        }

        public SetDifference(String date, TextView text) {

            super();
            this.date = date;
            this.text = text;
        }

        @SuppressLint("SimpleDateFormat")
        @Override
        protected String doInBackground(String... params) {

            String totaltimeDif = "";
            //
            try {
                // String t1= "yyyy-MM-dd'T'HH:mm:ss";
                String t2 = "dd-MM-yyyy HH:mm:ss";
                SimpleDateFormat df = new SimpleDateFormat(t2);
                Calendar cl = Calendar.getInstance();
                //
                df.setTimeZone(TimeZone.getTimeZone("UTC"));
                //
                String currentDateandTime = df.format(cl.getTime());
                //
                String newDate = date;
                //
                Date dateOne = df.parse(currentDateandTime);
                Date dateTwo = df.parse(newDate);
                //
                long difference = Math.abs(dateOne.getTime()
                            - dateTwo.getTime());
                //
                //
                int days = (int) TimeUnit.MILLISECONDS.toDays(difference);
                int hours = (int) TimeUnit.MILLISECONDS.toHours(difference);
                int min = (int) TimeUnit.MILLISECONDS.toMinutes(difference);
                int sec = (int) TimeUnit.MILLISECONDS.toSeconds(difference);
                //
                if (days > 0) {
                    if (days > 1 && days < 30) {
                        totaltimeDif = String.format(context.getResources().getString(R.string.string_date_days),days);
                    }
                    else
                        if (days == 1) {
                            totaltimeDif = String.format(context.getResources().getString(R.string.string_date_day),days);
                        }
                        else
                            if (days >= Month) {

                               totaltimeDif = newDate.split(" ")[0];
                            }
                }
                else
                    if (hours > 0) {
                        if (hours > 1) {
                            totaltimeDif = String.format(context.getResources().getString(R.string.string_date_hours),hours);
                        }
                        else {
                            totaltimeDif =String.format(context.getResources().getString(R.string.string_date_hour),hours);
                        }
                    }
                    else
                        if (min > 0) {
                            if (min > 1) {
                                totaltimeDif =String.format(context.getResources().getString(R.string.string_date_minutes),min);
                            }
                            else {
                                totaltimeDif =String.format(context.getResources().getString(R.string.string_date_minute),min);
                            }
                        }
                        else
                            if (sec > 0) {
                                totaltimeDif = context.getResources().getString(R.string.string_date_seconds);
                            }
                //
                //
                return totaltimeDif;
            }
            catch (Exception x) {
                return x.getMessage();
            }
        }

        @Override
        protected void onPostExecute(String result) {

            if (callback != null)
                callback.Completed(result);
            else
                text.setText(result);
        }
    }

    public UtilDate(Context context) {

        super();
        this.context = context;
    }

    @SuppressLint("SimpleDateFormat")
    public String convertDatetoCurrentZone(String date) {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date myDate = null;
        String formattedDate = "";
        try {
            myDate = simpleDateFormat.parse(date);
            simpleDateFormat.setTimeZone(TimeZone.getDefault());
            formattedDate = simpleDateFormat.format(myDate);
        }
        catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //
        return formattedDate;
    }

    @SuppressLint("SimpleDateFormat")
    public String convertTimetoCurrentZone(String time) {

        Calendar cl = Calendar.getInstance();

        TimeZone z = cl.getTimeZone();

        //
        SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");

        sdf2.setTimeZone(z);
        String result = sdf2.format(time);
        //
        return result;
    }

    public void setTimeDifference(String date, OnSetDifferenceCompleted callback) {

        SetDifference dif = new SetDifference(date, callback);
        dif.execute();
    }

    public void setTimeDifference(String date, TextView text) {

        SetDifference dif = new SetDifference(date, text);
        dif.execute();
    }
}
